import math
import sys
import io
import FileIO
import Probability
import Post
import Parser

PREDICTION_FILE_PATH = "F:\\Users\\Seph\\Datasets\\StackOverflowCompetition\\Results\\basic_benchmark.csv"
TEST_DATA_FILE_PATH = "F:\\Users\\Seph\\Datasets\\StackOverflowCompetition\\testingWithCloseData.csv"

def calculateLogLoss(probabilities):
	totalPosts = 0
	logTotal = 0
	for probability in probabilities:
		totalPosts+=1
		if(probability.status == Post.PostStatusEnum.OPEN):
			logTotal += math.log(probability.open)
		if(probability.status == Post.PostStatusEnum.OFF_TOPIC):
			logTotal += math.log(probability.offTopic)
		if(probability.status == Post.PostStatusEnum.NOT_CONSTRUCTIVE):
			logTotal += math.log(probability.notConstructive)
		if(probability.status == Post.PostStatusEnum.NOT_A_REAL_QUESTION):
			logTotal += math.log(probability.notARealQuestion)
		if(probability.status == Post.PostStatusEnum.TOO_LOCALIZED):
			logTotal += math.log(probability.tooLocalized)
	logLoss = float(-1)*((float(1)/float(totalPosts))* (logTotal))
	return logLoss

def main():
	testFileStream = FileIO.openFile(TEST_DATA_FILE_PATH, FileIO.READ_MODE)
	predictionFileStream = FileIO.openFile(PREDICTION_FILE_PATH, FileIO.READ_MODE)
	
	probabilities = []
	while (not Parser.isEndOfFile(testFileStream)):
		post = Parser.readPost(testFileStream)
		actualStatus =  post.openStatus
		postId = post.postID
		probability = Parser.readProbabilityLine(predictionFileStream,postId,actualStatus)
		
		probabilities.append(probability)
	
	print "Log Loss: " + str(calculateLogLoss(probabilities))
	
if __name__=="__main__":
    main()